<?php

class BBCode {

	/**
	 * Permet la convertion des tags BBCode en HTML
	 * La convertion est faite grâce aux regex 
	 * @return string le texte formaté
	 */
	public static function toHtml( $text)  {

		// contient les regex relatifs BBCode
		$BBCode = array(
		'~\\n~s',
		'~\[list\](.*?)\[/list\]~s',
		'~\[list=1\](.*?)\[/list\]~s',
		'~\[\*\](.*?)\[/\*\]~s',
		'~\[b\](.*?)\[/b\]~s',
		'~\[s\](.*?)\[/s\]~s',
		'~\[i\](.*?)\[/i\]~s',
		'~\[u\](.*?)\[/u\]~s',
		'~\[quote\](.*?)\[/quote\]~s',
		'~\[size=(.*?)\](.*?)\[/size\]~s',
		'~\[color=(.*?)\](.*?)\[/color\]~s',
		'~\[url\]((?:ftp|https?)://.*?)\[/url\]~s',
		'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s',
		'~\[right\](.*?)\[/right\]~s'
		);

		// contient les champs html correspondants
		$html = array(
		'<br/>',
		'<ul>$1</ul>',
		'<ol>$1</ol>',
		'<li>$1</li>',
		'<b>$1</b>',
		'<s>$1</s>',
		'<i>$1</i>',
		'<span style="text-decoration:underline;">$1</span>',
		'<pre>$1</'.'pre>',
		'<span style="font-size:$1px;">$2</span>',
		'<span style="color:$1;">$2</span>',
		'<a href="$1">$1</a>',
		'<img src="$1" alt="" />',
		'<span align:"right">$1</span>',
		);

		// Replacing the BBcodes with corresponding HTML tags
		return preg_replace($BBCode,$html,$text);
	}

}
